<template>
  <div>
    <div class="qualiy_list">
    <div class="monitor-tree">
      <h3 class="tree-title">选择洞幅</h3>
      <el-tree
        :data="tunnelInfoChild"
        @check-change="treeCheckClick"
        :check-on-click-node="true"
        node-key="pid"
        ref="tree"
        :default-expanded-keys="keysListNodeData"
        @node-click="nodetest"
      ></el-tree>
    </div>

    <div class="monitor-list">
      <QuailtyAdminHeader />
      <el-table  :data="projectInfo" :header-cell-style="{ background: '#f8f8f9',textAlign: 'center' }" :cell-style="{'text-align':'center'}">
        <el-table-column  width="190"  show-overflow-tooltip prop="reportNo" label="报告编号"></el-table-column>
        <el-table-column width="90"  show-overflow-tooltip prop="startMileage" label="开始里程"></el-table-column>
        <el-table-column width="90"  show-overflow-tooltip prop="endMileage" label="结束里程"></el-table-column>
        <el-table-column width="120"  show-overflow-tooltip prop="mileageLength" label="检测长度(米)"></el-table-column>
        <el-table-column  show-overflow-tooltip prop="mileageContext" label="检测内容"></el-table-column>
        <el-table-column    show-overflow-tooltip prop="result" label="检测结果">
          <template slot-scope="scope" >
            <el-tag v-if="scope.row.result == 2"  @click="clikeRow(scope.row)" type="warning">不合格</el-tag>
            <el-tag v-if="scope.row.result == 1"   type="success">合格</el-tag>
          </template>
        </el-table-column>
        <el-table-column  :formatter="formatDate"  show-overflow-tooltip prop="createTime" label="报告日期"></el-table-column>
        <el-table-column width="300" show-overflow-tooltip label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="preview(scope.$index, scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="info"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
            @current-change="handleCurrentChange"
            layout="total, sizes, prev, pager, next, jumper"
            :total="currentPage.total"
        ></el-pagination>
      </div>
    </div>
    <QuailtyAdminEditor :flag="flag" />

    <!-- 分页 -->
    <el-alert closable @close="unqualifiedcloseHandler"  class="unqualified-alert" v-if="unqualifiedShow"
    :title= "unqualified"
    type="error">
  </el-alert>
  </div>
  </div>
</template>

<script>
import api from '../../../api'
import base from '../../../api/base'
import qs from 'qs'
import QuailtyAdminEditor from './QuailtyAdminEditor.vue'
import QuailtyAdminHeader from './QuailtyAdminHeader.vue'
import { mapState } from 'vuex'
import pdfTemp from "../../pdf/pdf.vue";

export default {
  data() {
    return {
      keysListNodeData:[],
      unqualified:'',
      unqualifiedShow:false,
      wId: '', // 洞幅id
      tunnelInfoChild: [],
      projectInfo: [],
      flag: false,
      currentPage: {
        pageNo: 1,
        total: 0
      }
    }
  },
  components: {
    QuailtyAdminEditor,
    QuailtyAdminHeader,
    pdfTemp
  },
  computed: {
    ...mapState('AddrPlaneModule', ['tunName']),
    tunNameChange() {
      return this.tunName
    }
  },
  mounted() {
    this.getchildren()
    this.$bus.$on('onRefreshQuailtyAdmin', (flag) => {
      this.currentPage.pageNo = flag.pageNo
      this.http(this.currentPage.pageNo, flag)
    })
    this.$bus.$on('onAddQuailtyListShow', (flag) => {
      console.log(flag)
      this.projectInfo = flag
    })

  },
  methods: {
    //获取数结构
    getchildren() {
      api
        .tunnelWidthFindByClid()
        .then((res) => {
          if (res.data.code == 2001) {
            this.tunnelInfoChild = res.data.data
            const arrDat = res.data.data[0].pid
            this.keysListNodeData.push(arrDat)
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    http(page, wId ,tId) {
      api
        .monitorMeasurepageList({ page, wId, testType: '初支检测' ,tId })
        .then((res) => {
          if (res.data.code == 2001) {
            this.projectInfo = res.data.data.records
            this.currentPage.total = parseInt(res.data.data.total)
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    handleEdit(index, row) {
      this.$bus.$emit('onShowQuailtyEditor', { rowdata: row, wId: this.wId,pageNo:this.currentPage.pageNo })
    },
    handleDelete(index, row) {
      let data = {
        id: row.id
      }
      this.$confirm('此操作将永久删除初支检测报告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'del-model',
        type: 'warning'
      })
        .then(() => {
          api
            .monitorMeasuredeleteById(qs.stringify(data))
            .then((res) => {
              if (res.data.code == 2001) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                // 视图重绘
                this.http(1,this.wId)
              } else {
                this.$message({
                  type: 'fail',
                  message: '删除失败!'
                })
              }
            })
            .catch((err) => {
              console.log(err)
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    preview(index, row) {
      if(row.resources == ''){
         this.$message({
                  type: 'fail',
                  message: '附件为空!'
                })
      }else{
       this.pdfsrc = base.filebaseUrl + row.resources
       this.$router.push('/pdf/' + encodeURIComponent(this.pdfsrc))
      }
    },
    handleCurrentChange(val) {
      this.http(val, this.wId)
    },
    treeCheckClick(data, checked, node) {
      if (data.id != undefined) {
        this.wId = data.id
        this.http(1, this.wId)
        this.$bus.$emit('onAddQuailtyWid', this.wId)
      }
    },
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property]
      if (data == null) {
        return null
      }
      let dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    },
    clikeRow(row, column, event){
      if (row.result == 2){
        this.unqualified = row.unqualified
        this.unqualifiedShow = true
      }
    },
    unqualifiedcloseHandler(){
      this.unqualifiedShow = false
    },
    nodetest(data, checked, node){
      if (data.tid != undefined) {
        this.http(1,null,data.tid)
      }
    }
  }
}
</script>

<style scoped>


.tree-title {
  text-align: left;
  font-size: 16px;
  color: #606266;
  padding: 10px;
  padding-bottom: 20px;
}
.monitor-tree {
  float: left;
  width: 300px;
  border-left: 1px solid #f1f1f1;
  border-right: 1px solid #f1f1f1;
  background-color: #fff;
}
.monitor-list {
  float: right;
  margin-left: 10px;
  width: 100%;
}
.unqualified-alert{
  position: fixed;
  top: 80px;
  left: 50%;
  z-index: 10000;
  color: red;
  width: 500px;
  margin-left: -250px;
}

</style>